package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.chenenyu.router.annotation.Route;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.dialog.ItemPopWindow;
import sz.szsmk.citizencard.model.network.dzbean.res.GetOpenIdRes;
import sz.szsmk.citizencard.model.network.dzbean.res.User;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.MainActMvpView;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.MainActPresenter;
import sz.szsmk.citizencard.ui.activity.WebViewActivity;
import sz.szsmk.citizencard.ui.dzsmkfragment.BillFragment;
import sz.szsmk.citizencard.ui.dzsmkfragment.MainFragment;
import sz.szsmk.citizencard.ui.dzsmkfragment.MineFragment;
import sz.szsmk.citizencard.ui.dzsmkfragment.OnFragmentRefresh;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.ForceExit;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.PubUtils;
import sz.szsmk.citizencard.widget.NoScrollViewPager;

@Route("dzsmk")
public class MainActivity extends AppBaseActivity implements MainActMvpView {

    NoScrollViewPager vp_content;
    ImageView iv_qr_right;


    View v_qr_main_line;
    View v_qr_bill_line;
    View v_qr_mine_line;

    CheckBox cb_qr_main;
    CheckBox cb_qr_bill;
    CheckBox cb_qr_mine;


    @Inject
    MainActPresenter mMainActPresenter;

    private List<Fragment> fragments;
    private MainFragment mMainFragment;
    private BillFragment billFragment;
    private MineFragment mineFragment;

    private List<CheckBox> mCheckBoxs;
    private int select = 0;
//    private TextView topview;

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getBooleanExtra("finish", false)) {
            if (ForceExit.getInstance() != null) {
                ForceExit.getInstance().logout();
            }
            finish();
//            getFragmentManager().popBackStack();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(MainActivity.this);
        setContentView(R.layout.qr_activity_main);

        initView();

        iv_qr_right.setVisibility(View.VISIBLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        String userId = getIntent().getStringExtra(Constant.USER_ID);
        String mobileNumber = getIntent().getStringExtra(Constant.MOBILE_NUMBER);
        String photoUrl = getIntent().getStringExtra(Constant.PHOTO_URL);


        mCheckBoxs = new ArrayList<>();
        mCheckBoxs.add(cb_qr_main);
        mCheckBoxs.add(cb_qr_bill);
        mCheckBoxs.add(cb_qr_mine);


        fragments = new ArrayList<>();
        mMainFragment = new MainFragment();
        billFragment = new BillFragment();
        mineFragment = new MineFragment();

        fragments.add(mMainFragment);
//        fragments.add(billFragment);
//        fragments.add(mineFragment);

        vp_content.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });


        vp_content.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int i) {
                Fragment fragment = fragments.get(i);
                if (fragment instanceof OnFragmentRefresh) {
                    OnFragmentRefresh refreshFragment = (OnFragmentRefresh) fragment;
                    refreshFragment.onRefresh();
                }
                for (int j = 0; null != mCheckBoxs && j < mCheckBoxs.size(); j++) {
                    CheckBox checkBox = mCheckBoxs.get(j);
                    if (i == j) {
                        checkBox.setChecked(true);
                    } else {
                        checkBox.setChecked(false);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });

        vp_content.setOffscreenPageLimit(6);
        vp_content.setCurrentItem(0, false);

        showMenuLine();
        mMainActPresenter.getOpenId();
//        mMainActPresenter.getPersonInfo();

//        mMainFragment.setPreAuthArgs(userId, mobileNumber, photoUrl);
//        mMainActPresenter.preAuth(userId, mobileNumber, photoUrl);
    }

    private void initView() {
        vp_content = findViewById(R.id.vp_qr_content);
        iv_qr_right = findViewById(R.id.iv_qr_right);

        v_qr_main_line= findViewById(R.id.v_qr_main_line);
        v_qr_bill_line= findViewById(R.id.v_qr_bill_line);
        v_qr_mine_line= findViewById(R.id.v_qr_mine_line);

        cb_qr_main= findViewById(R.id.cb_qr_main);
        cb_qr_bill= findViewById(R.id.cb_qr_bill);
        cb_qr_mine= findViewById(R.id.cb_qr_mine);


        findViewById(R.id.rl_qr_main).setOnClickListener(this::onClickView);
        findViewById(R.id.rl_qr_bill).setOnClickListener(this::onClickView);
        findViewById(R.id.rl_qr_mine).setOnClickListener(this::onClickView);
        cb_qr_main.setOnClickListener(this::onClickView);
        cb_qr_bill.setOnClickListener(this::onClickView);
        cb_qr_mine.setOnClickListener(this::onClickView);
        iv_qr_right.setOnClickListener(this::onClickView);

    }

    @Override
    protected CharSequence getTopTitle() {
        return "扫码乘车";
    }

    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{mMainActPresenter};
    }


    public void onClickView(View v) {
        if (v.getId() == R.id.rl_qr_main || v.getId() == R.id.cb_qr_main) {
            vp_content.setCurrentItem(0, false);
            mMainFragment.setMainFragmentVisible(true);
            cb_qr_main.setChecked(true);
            showMenuLine();
            showQrNavMenu();

        } else if (v.getId() == R.id.rl_qr_bill || v.getId() == R.id.cb_qr_bill) {
            vp_content.setCurrentItem(1, false);
            mMainFragment.setMainFragmentVisible(false);
            cb_qr_bill.setChecked(true);
            showMenuLine();
            showQrNavMenu();

        } else if (v.getId() == R.id.rl_qr_mine || v.getId() == R.id.cb_qr_mine) {
            iv_qr_right.setVisibility(View.VISIBLE);
            vp_content.setCurrentItem(2, false);
            mMainFragment.setMainFragmentVisible(false);
            cb_qr_mine.setChecked(true);
            showMenuLine();
        } else if (v.getId() == R.id.iv_qr_right) {
            int currentItem = vp_content.getCurrentItem();
            if (1 == currentItem) {
                showFilterPopDialog();
            } else if (0 == currentItem) { //首个tab
                showHelpPopDialog();
            }
        }
    }

    public void showQrNavMenu() {
        int currentItem = vp_content.getCurrentItem();
        if (0 == currentItem) {
            iv_qr_right.setImageResource(R.drawable.qr_nav_more);
        } else {
            iv_qr_right.setImageResource(R.drawable.qr_nav_more);
        }
        iv_qr_right.setVisibility(View.VISIBLE);
    }

    private void showMenuLine() {
        v_qr_main_line.setVisibility(View.INVISIBLE);
        v_qr_bill_line.setVisibility(View.INVISIBLE);
        v_qr_mine_line.setVisibility(View.INVISIBLE);
        int currentItem = vp_content.getCurrentItem();
        if (0 == currentItem) {
            v_qr_main_line.setVisibility(View.VISIBLE);
        } else if (1 == currentItem) {
            v_qr_bill_line.setVisibility(View.VISIBLE);
        } else {
            v_qr_mine_line.setVisibility(View.VISIBLE);

        }
    }

    @Override
    public void onPause() {
        super.onPause();
        int currentItem = vp_content.getCurrentItem();
        if (mMainFragment.isShowingRefreshQrCode() && 0 == currentItem) {
            mMainFragment.setMainFragmentVisible(false);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        int currentItem = vp_content.getCurrentItem();
        if (!mMainFragment.isShowingRefreshQrCode() && 0 == currentItem) {
            mMainFragment.setMainFragmentVisible(true);
        }

    }

    private void showHelpPopDialog() {
        ItemPopWindow.PopItem popItem = new ItemPopWindow.PopItem();
        popItem.setName("刷新");
        popItem.setPosition(0);

        ItemPopWindow.PopItem popItem2 = new ItemPopWindow.PopItem();
        popItem2.setName("使用说明");
        popItem2.setPosition(1);

        ItemPopWindow.PopItem popItem3 = new ItemPopWindow.PopItem();
        popItem3.setName("我的钱包");
        popItem3.setPosition(2);

        List<ItemPopWindow.PopItem> popItems = new ArrayList<>();
        popItems.add(popItem);
        popItems.add(popItem2);
        popItems.add(popItem3);

        ItemPopWindow popWindow = new ItemPopWindow(MainActivity.this, new ItemPopWindow.OnItemClickListener() {
            @Override
            public void onItemClick(ItemPopWindow.PopItem popItem) {
                if (0 == popItem.getPosition()) { //1 取消后，还在当前页面，恢复
                    mMainFragment.setMainFragmentVisible(true);
                } else if (popItem.getPosition() == 1) {
                    launch(WebViewActivity.class);
                }else if (popItem.getPosition() == 2){

                    Bundle bundle1 = new Bundle();
                    bundle1.putString("type", "1");
                    JumpUtils.jumptoNatvie(MainActivity.this, "dzsmkuser", 0, bundle1);
                }
            }
        }, popItems);

        popWindow.showAtLocation(findViewById(R.id.ll_qr_main),
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        mMainFragment.setMainFragmentVisible(false);
    }

    private void showFilterPopDialog() {
        ItemPopWindow.PopItem popItem = new ItemPopWindow.PopItem();
        popItem.setName("全部");
        popItem.setPosition(0);

        ItemPopWindow.PopItem popItem2 = new ItemPopWindow.PopItem();
        popItem2.setName("消费");
        popItem2.setPosition(1);

        ItemPopWindow.PopItem popItem3 = new ItemPopWindow.PopItem();
        popItem3.setName("充值");
        popItem3.setPosition(2);

        List<ItemPopWindow.PopItem> popItems = new ArrayList<>();
        popItems.add(popItem);
        popItems.add(popItem2);
        popItems.add(popItem3);

        ItemPopWindow popWindow = new ItemPopWindow(MainActivity.this, new ItemPopWindow.OnItemClickListener() {
            @Override
            public void onItemClick(ItemPopWindow.PopItem popItem) {
                if (0 == popItem.getPosition()) { //1 取消后，还在当前页面，恢复
                    select = 0;
                    billFragment.initData(2);
                } else if (1 == popItem.getPosition()) {
                    select = 1;
                    billFragment.initData(1);
                } else if (2 == popItem.getPosition()) {
                    select = 2;
                    billFragment.initData(0);
                }
            }
        }, popItems, select);

        popWindow.showAtLocation(findViewById(R.id.ll_qr_main),
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        mMainFragment.setMainFragmentVisible(false);


    }

    @Override
    public void onPreAuthSuccess() {
        mMainFragment.preAuthSuccess();
    }

    @Override
    public void onPreAuthFailure() {
        PubUtils.popTipOrWarn(this, "认证失败，请重试！");
        finish();
    }

    @Override
    public void getOpenIdFail() {

    }

    @Override
    public void onGainPersonInfo(User user) {
        String idCardNumber = user.getIdCardNumber();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ACache.get(this).remove("channelId");
    }

    @Override
    public void getOpenIdSucc(GetOpenIdRes response) {
        String openId = ACache.get(this).getAsString("openId");
        String mobile = ACache.get(this).getAsString("mobile");
        mMainActPresenter.preAuthThird(openId, mobile, response.getUserId());
    }

}
